// 收藏接口
const express = require('express')
// 引入连接池模块
const pool = require('../pool.js')
const router = express.Router()

// 收藏添加
// http://127.0.0.1:3000/sctj
router.post('/',(req,res,next) => {
    var obj=req.body
    pool.query('insert into qnls_collect set ?',[obj],(err,r) => {
        if(err){
            return next(err)
        }
        res.send({
            code:200,msg:'添加成功',data:r
        })
    })
})

// 通过用户id关联查询房源
// http://127.0.0.1:3000/sctj/uesrid
router.post('/userid',(req,res,next)=>{
    var obj=req.body
	console.log(obj)
    pool.query('select * from qnls_collect c left join qnls_hotellist r on r.hid=c.fy where c.userid=?',[obj.userid],(err,r)=>{
        if(err) {
            return next(err)
        }
        res.send({code: 200,msg: '查询成功',data: r})
    })
})

// 删除收藏
// http://127.0.0.1:3000/sctj/del
router.post('/del',(req,res,next)=>{
    var obj=req.body
  console.log(obj)
    pool.query('delete from qnls_collect where cid=?',[obj.cid],(err,r)=>{
        if(err){
      throw err
    }
        if(r.affectedRows===0){
            res.send({code:400,msg:'删除失败'})
        }else{
            res.send({code:200,msg:'删除成功'})
        }
    })
  })

module.exports = router